package main.pmc;

import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.alibaba.fastjson.JSONArray;
import lombok.SneakyThrows;
import utils.StringUtil;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static main.pmc.DownPmcFile.getFileName;

/**
 * @Author: 葛成瑞
 * @Date: 2025/3/5 16:49
 */
public class LocalPmcFileCheck4 {
    @SneakyThrows
    public static void main(String[] args) throws SQLException {

        // 尝试删除没用数据 防止误删除 先校验查看下
        String rootPath = "F:\\pmcdownextra\\";
        Db use = Db.use();
//        List<Entity> query = use.query("select * from pmcdown_new2 where pmid = 19700551 ");
        List<Entity> query = use.query("select * from pmcdown_new2  ");
        for (Entity entity : query) {
            Integer pmid = entity.getInt("pmid");
            String secondPath =  "/" + pmid / 50000 + 1 + "/";
            String sen = rootPath + secondPath + pmid + "\\";
            String str = entity.getStr("supp");
            Set<String> allPath = new HashSet<>();
            if (StringUtil.isNotNull(str)) {
                List<SaveEntity> ls = JSONArray.parseArray(str, SaveEntity.class);
                List<File> allFileDown = new ArrayList<>();
                List<File> AllFile = FileUtil.loopFiles(sen);
                for (SaveEntity saveEntity : ls) {
                    Integer code = saveEntity.getCode();
                    String url = saveEntity.getUrl();
                    String fileName = getFileName(url);
                    File file = new File(sen + fileName);
                    allPath.add(file.getPath());
                    if (code != null && code.intValue() == 200) {
                        allFileDown.add(file);
                    }
                }
                if (AllFile.size() != allFileDown.size()) {
                    // 进行删除没用数据
                    // 删除没用的数据 首先是文件夹
                    for (File file : AllFile) {
                        if (file.isDirectory()) {
                            System.out.println("删除文件夹？+"+file.getPath());

//                            FileUtil.del(file);
                        }else {
                            if (!allPath.contains(file.getPath())){
                                System.out.println("删除多余文件++"+file.getPath());
                                try {
                                    FileUtil.del(file);
                                }catch (Exception e){
                                    e.printStackTrace();

                                }

                            }
                        }
                    }

                    System.out.println("down size+"+allFileDown.size());
                    System.out.println("AllFile size+"+AllFile.size());
                    System.out.println("有问题++"+pmid);
                    System.out.println("有问题++"+sen);
                    System.out.println("+++++++++++++++++++++++++++++++++++++"+pmid);
                    Thread.sleep(2000);
                }else {
//                    System.out.println("没问题");
                }
            }
        }
    }



}
